
//  JQFYPlayListManger.m
//  JQFY
//
//  Created by 代辉 on 2017/8/18.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYPlayListManger.h"
#import "JQFYPlayList.h"
#import "JQFYSong.h"
#import <AVFoundation/AVFoundation.h>
#import "JQFYMusicPlayer.h"

@interface JQFYPlayListManger ()

@property (nonatomic,copy) NSString *cacheLrcPath;

@end

@implementation JQFYPlayListManger {
    NSMutableArray<JQFYPlayList *>  *_playlists;
    JQFYPlayList *_playlist;
    NSArray *_musics;
}


IMPLEMENTATION_SINGLETON(JQFYPlayListManger)

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        // 初始化代码
        _playlists = [NSMutableArray array];
    }
    return self;
}

#pragma mark - 获取音乐列表 -
- (void)getMusicData:(MusicData)sussce failure:(MusicData)faliure
{
    [[JQFYNetworkTool sharedManager] request:RequestMethodGet
                                   urlString:@"http://app.uninhibited.cn/api/song/list"
                                  parameters:nil
                                    finished:^(id responseObject, NSError *error) {
                                        
                                        if (error) {
                                            if (faliure) {
                                                faliure();
                                            }
                                        } else {
                                            _playlist = [JQFYPlayList new];
                                            NSArray *musicSongList = responseObject;
                                            NSArray *songList = [NSArray yy_modelArrayWithClass:[JQFYSong class] json:musicSongList];
                                            for (JQFYSong *song in songList) {
                                                [_playlist addSong:song];
                                            }
                                            [self addPlaylist:_playlist];
                                            
                                            sussce();
                                        }
                                        
                                    }];
}


- (void)getHomeRecommendMusic:(void (^)(NSArray *))sucess file:(void (^)(NSError *))file
{
    [[JQFYNetworkTool sharedManager] request:RequestMethodGet
                                   urlString:@"http://app.uninhibited.cn/api/music/top"
                                  parameters:nil
                                    finished:^(id responseObject, NSError *error) {
                                        if (error) {
                                            if (file) {
                                                
                                                file(error);
                                            }
                                        } else {
                                            
                                            _musics = [NSArray yy_modelArrayWithClass:[JQFYSong class] json:responseObject];
                                            __Player__.recommendMusics = _musics;
                                            sucess(_musics);
                                        }
                                    }];
}

#pragma mark - 播放列表管理 -
// 获取所有的播放列表
- (NSArray<JQFYPlayList *>*)allPlaylists {
    return _playlists;
}

// 添加播放列表
- (void)addPlaylist:(JQFYPlayList *)playlist {
    if(playlist == nil) return;
    [_playlists addObject:playlist];
}

// 删除播放列表通过索引
- (void)removePlaylistAtIndex:(NSUInteger)index {
    if(_playlists.count == 0 || index >= _playlists.count) return;
    [_playlists removeObjectAtIndex:index];
}


@end
